package sk.tuke.homescreen;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Calendar;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;

public class LauncherActivity extends Activity {

    AppInfoHolder mInfoAdapter = null;
    private String TAG = "HomeScreen";
    private Tts mSpeechEngine = null;
    private Vibrator mVibrator = null;
    private static GeneralContext mGeneralContext = null;
    
    private static IntentFilter s_intentFilter;
    static{
    	s_intentFilter = new IntentFilter();
        s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
        s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	Log.i(TAG, "TestTag");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
        mSpeechEngine = new Tts(this);
        mGeneralContext = new GeneralContext();
        mVibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
        mGeneralContext.setActivity(this);
        mGeneralContext.setSpeechEngine(mSpeechEngine);
        mGeneralContext.setVibrateService(mVibrator);
        mInfoAdapter = new AppInfoHolder(getPackageManager());
        //mInstance = this;
        try{
	        if (savedInstanceState == null) {
	            FragmentTransaction t = getFragmentManager().beginTransaction();
	            t.add(R.id.container, new MenuFragment()).commit();
	        }
        }catch(Exception ex){
        	Log.e(TAG, ex.getMessage());
        }

    }

    @Override
    protected void onStart(){
        Log.i(TAG, "onStart entered");
        if(mSpeechEngine == null){
        	mSpeechEngine = new Tts(this);
        	mGeneralContext.setSpeechEngine(mSpeechEngine);
        }
        mVibrator.vibrate(500);
        Calendar c = Calendar.getInstance();
        final int minutes = c.get(Calendar.MINUTE);
        final int hour = c.get(Calendar.HOUR);
        mSpeechEngine.sayMessage(String.valueOf(hour) + " o clock " + String.valueOf(minutes) + " minutes");
        Log.i(TAG, "onStart left");
        super.onStart();
    } 
    
    @Override
    protected void onStop(){
        //mSpeechEngine.dispose();
        //mSpeechEngine = null;
        super.onStop();
    }
    
    public static GeneralContext getGeneralContext(){
    	return mGeneralContext;
    }
    
    @Override
    public void onBackPressed(){
    	Log.d(TAG, "onBackPressed");
    }

}